package com.jf.dao.impl;

import com.jf.bean.Banner;
import com.jf.dao.BannerDao;
import com.jf.util.BaseDao;
import com.jf.util.MyUtils;
import com.jf.util.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class BannerDaoImpl implements BannerDao {

    @Override
    public List<Banner> getPage(PageInfo pageInfo, Banner banner) {
        List<Object> objs = new ArrayList<>();
        String sql = "select * from banner where 1 = 1 ";
        if(MyUtils.isStringNotEmpty(banner.getName())){
            sql+="and name like concat(\"%\",?,'%') ";
            objs.add(banner.getName());
        }
        if(banner.getStatus() != null){
            sql+="and status = ? ";
            objs.add(banner.getStatus());
        }
        sql+="order by id desc limit ?,?";
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(Banner.class,sql,objs);
    }

    @Override
    public int getCount(Banner banner) {
        List<Object> objs = new ArrayList<>();
        String sql = "select count(id) from banner where 1 = 1 ";
        if(MyUtils.isStringNotEmpty(banner.getName())){
            sql+="and name like concat(\"%\",?,'%') ";
            objs.add(banner.getName());
        }
        if(banner.getStatus() != null){
            sql+="and status = ? ";
            objs.add(banner.getStatus());
        }
        return BaseDao.baseQueryCount(sql,objs);
    }

    @Override
    public int addSelect(Banner banner) {
        List<Object> objs = new ArrayList<>();
        String sql = "insert into banner(";
        if(banner.getId()!= null){
            sql+="id,";
        }
        if(MyUtils.isStringNotEmpty(banner.getName())){
            sql+="name,";
        }
        if(MyUtils.isStringNotEmpty(banner.getSrc())){
            sql+="src,";
        }
        if(MyUtils.isStringNotEmpty(banner.getHref())){
            sql+="href,";
        }
        if(banner.getStatus() != null){
            sql+="status,";
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=") values(";
        if(banner.getId()!= null){
            sql+="?,";
            objs.add(banner.getId());
        }
        if(MyUtils.isStringNotEmpty(banner.getName())){
            sql+="?,";
            objs.add(banner.getName());
        }
        if(MyUtils.isStringNotEmpty(banner.getSrc())){
            sql+="?,";
            objs.add(banner.getSrc());
        }
        if(MyUtils.isStringNotEmpty(banner.getHref())){
            sql+="?,";
            objs.add(banner.getHref());
        }
        if(banner.getStatus() != null){
            sql+="?,";
            objs.add(banner.getStatus());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=")";
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int updateSelect(Banner banner) {
        List<Object> objs = new ArrayList<>();
        String sql = "update banner set ";
        if(MyUtils.isStringNotEmpty(banner.getName())){
            sql+="name = ?,";
            objs.add(banner.getName());
        }
        if(MyUtils.isStringNotEmpty(banner.getSrc())){
            sql+="src = ?,";
            objs.add(banner.getSrc());
        }
        if(MyUtils.isStringNotEmpty(banner.getHref())){
            sql+="href = ?,";
            objs.add(banner.getHref());
        }
        if(banner.getStatus() != null){
            sql+="status = ?,";
            objs.add(banner.getStatus());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=" where id = ?";
        objs.add(banner.getId());
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int batchDelete(int[] ids) {
        List<Object> objs = new ArrayList<>();
        StringBuilder sql = new StringBuilder("delete from banner where id in(");
        for(int id : ids){
            sql.append("?,");
            objs.add(id);
        }
        sql=new StringBuilder(sql.substring(0,sql.lastIndexOf(",")));
        sql.append(")");
        return BaseDao.baseBatch(sql.toString(),ids.length,objs);
    }

    @Override
    public Banner getById(int id) {
        String sql = "select * from banner where id = ?";
        return BaseDao.baseQueryBean(Banner.class,sql,id);
    }

    @Override
    public List<Banner> getList() {
        String sql = "select * from banner where status = 1 order by id desc";
        return BaseDao.baseQuery(Banner.class,sql);
    }
}
